package interactive.states
{
	import events.GestureEvent;
	
	import flash.events.TimerEvent;
	import flash.geom.Point;
	
	import global.Managers;
	
	import interactive.GestureManager;
	
	import utils.EnterFrameTimer;

	/**
	 *
	 * 单击状态
	 * 1. 在双击延迟之前没有触摸屏幕则变为离开状态
	 * 2. 在双击延迟之前触摸了屏幕且位置与前一次点击距离不大于最小手势距离，
	 * 则进入单击触摸状态
	 * 3. 在双击延迟之前触摸了屏幕且位置与前一次点击距离大于最小手势距离，
	 * 则进入触摸状态
	 *  
	 * @author Raykid
	 * 
	 */
	public class ClickState implements IGestureState
	{
		private var _lastPoint					: Point;
		private var _timerDoubleClick			: EnterFrameTimer;
		
		public function ClickState()
		{
			_lastPoint = new Point();
			
			_timerDoubleClick = new EnterFrameTimer(GestureManager.DOUBLE_CLICK_DELAY, 1);
			_timerDoubleClick.addEventListener(TimerEvent.TIMER_COMPLETE, timerDoubleClickCompleteHandler)
		}
		private function timerDoubleClickCompleteHandler(event:TimerEvent):void {
			// 1. 在双击延迟之前没有触摸屏幕则变为离开状态
			var manager:GestureManager = Managers.gestureManager;
			manager.switchState(manager.leaveState, _lastPoint);
		}

		public function backIn(manager:GestureManager, point:Point):void {
			// 无操作
		}
		public function switchIn(manager:GestureManager, point:Point):void
		{
			_lastPoint.x = point.x;
			_lastPoint.y = point.y;
			_timerDoubleClick.start();
			// 触发单击事件
			manager.downDispatcher.dispatchEvent(new GestureEvent(GestureEvent.CLICK, manager.downTarget, true, false, _lastPoint.x, _lastPoint.y));
		}
		public function switchOut(manager:GestureManager, point:Point):void
		{
			_timerDoubleClick.reset();
		}
		public function touch(manager:GestureManager, point:Point):void
		{
			var distance:Number = Point.distance(point, _lastPoint);
			if(distance <= GestureManager.HAND_DISTANCE_MIN) {
				// 2. 在双击延迟之前触摸了屏幕且位置与前一次点击距离不大于最小手势距离，则进入单击触摸状态
				manager.switchState(manager.clickTouchState, point);
			} else {
				// 3. 在双击延迟之前触摸了屏幕且位置与前一次点击距离大于最小手势距离，则进入触摸状态
				manager.switchState(manager.touchState, point);
			}
		}
		public function leave(manager:GestureManager, point:Point):void
		{
			// 无操作
		}
		public function move(manager:GestureManager, point:Point):void {
			// 无操作
		}
		public function toString():String {
			return "ClickState";
		}
	}
}